package edu.sctu.wangfuxin.day20150628.until;

import edu.sctu.wangfuxin.day20150628.data.JsonData;

import java.io.*;


/**
 * Created by user06 on 2015/6/28.
 */

/**
 * 实现序列化与反序列化类
 */
public class SerializableUtil {

    private String path;

    public SerializableUtil(String path) {
        this.path = path;
    }

    /**
     * 序列化对象
     * @param obj
     */

    public void serializableClass(Object obj) {
        try {
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
            oos.writeObject(obj);
            oos.flush();
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 反序列化对象
     *
     * @return
     */
    public Object antiSerializableClass() {
        Object obj = null;
        try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
            try {
                obj = ois.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return obj;
    }
}
